import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class ViewerOverMap {
	
	Tiles t;
	Maps m;
	
	JPanel painelMapaMain;
	JPanel painelTile;
	JPanel painelMap = new JPanel();
	JPanel painelConfig = new JPanel();
	
	JButton getMap = new JButton("Get OverMap");
	JButton buildMap = new JButton("Build Map");
	JButton deselect = new JButton("Cancel Tile");
	ViewerMapaMain vmp;
	JScrollPane jspTile;
	JScrollPane jspMap;
	ViewerOverMap vom = this;
	
	JToggleButton btnmap[][];
	
	String path;
	
	
	public ViewerOverMap(JPanel painelOverMap) {
		painelTile = new JPanel();
		this.painelMapaMain = painelOverMap;
		//this.vmp = Viewer.getVmp();
		//System.out.println(vmp);
		painelOverMap.setLayout( new BorderLayout() );
		
		painelTile.setBorder(new TitledBorder ( new EtchedBorder (), "Tile Area" ) );
		painelMap.setBorder(new TitledBorder ( new EtchedBorder (), "Map Area" ) );
		painelConfig.setBorder(new TitledBorder ( new EtchedBorder (), "Config Area" ) );
		
		createPainelTile();
	    createPainelConfig();
	    
	    painelOverMap.add(jspTile, BorderLayout.NORTH);
	    painelOverMap.add(painelConfig, BorderLayout.SOUTH);
		painelOverMap.add(jspMap,BorderLayout.CENTER);
		
		//System.out.println("JA FUI CRIADO");
	}

	

	private void createPainelTile() {
//painelTile.setPreferredSize(new Dimension(100,150));
	    jspTile = new JScrollPane(painelTile);
	    painelTile.add(getMap);
	    
	    getMap.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {	
				path = Utils.getMappath();
				
				if(path!=null){
					try {
						t = new Tiles(vom) ;
						//m = new Maps(vom);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					//ImageIcon ii = new ImageIcon(path);
			    	//JButton btn = new JButton(ii);
			    	//btn.setBorder(null);
				    //painelTile.add(btn);
					painelTile.validate();
				}
				
				
//				mapTile = new BufferedImage(ii.getIconWidth(), ii.getIconHeight(), BufferedImage.TYPE_INT_RGB);
//				Graphics g = mapTile.createGraphics();
//				ii.paintIcon(null, g, 0,0);
//				g.dispose();
				
				//BufferedImage teste[][] = Utils.splitImage(mapTile, 1, 5);
			}
		});
	    jspTile.setPreferredSize(new Dimension(100,150) );
		
	}
	
	private void createPainelConfig() {
		jspMap = new JScrollPane(painelMap);
		painelConfig.setLayout( new FlowLayout() );
		
		buildMap.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				final JFileChooser exportFileChooser = new JFileChooser();
				exportFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				exportFileChooser.setApproveButtonText("Export");
				 int returnVal = exportFileChooser.showSaveDialog(buildMap.getParent());
				 if (returnVal == JFileChooser.APPROVE_OPTION) {
				        File outputFile = exportFileChooser.getSelectedFile();
				        if (outputFileIsValid(outputFile)) {
				        	if(m!=null && Utils.getChoose() != null){
				        	try {
				        		vmp = Viewer.getVmp();
				        		//System.out.println(vmp);
								new Impressao(vmp,vom, outputFile);
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
				        	}else{
				        		JOptionPane.showConfirmDialog(null,"Erro, Sem Mapa Instaciado");
				        	}
				        }
				    }
			}

			private boolean outputFileIsValid(File outputFile) {
				boolean fileIsValid = false;
		        if (outputFile.exists()) {
		            int result = JOptionPane.showConfirmDialog(
		                    buildMap.getParent(),
		                    "File exists, overwrite?", "File exists",
		                    JOptionPane.YES_NO_CANCEL_OPTION);
		            switch (result) {
		            case JOptionPane.YES_OPTION:
		                fileIsValid = true;
		                break;
		            default:
		                fileIsValid = false;
		            }
		        } else {
		            fileIsValid = true;
		        }
		        return fileIsValid;
		    }
		});
		
		painelConfig.add(buildMap);
		//painelConfig.add(deselect);
		painelConfig.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
	}



	public String getPath() {
		// TODO Auto-generated method stub
		return path;
	}
	
public ViewerOverMap getVom() {
	return vom;
}
}
